package by.fedorenko.sample.services.impl;

import javax.annotation.PostConstruct;
import javax.inject.Inject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import by.fedorenko.sample.dataaccess.RezervationDao;
import by.fedorenko.sample.datamodel.Rezervation;
import by.fedorenko.sample.services.RezervationService;

public class RezervationServiceImpl implements RezervationService {
	private static final Logger LOGGER = LoggerFactory.getLogger(RezervationServiceImpl.class);
	@Inject
	private RezervationDao rezervationDao;
	@PostConstruct
	private void init() {
		// this method will be called by Spring after bean instantiation. Can be
		// used for any initialization process.
		LOGGER.info("Instance of RezervationService is created. Class is: {}", getClass().getName());
	}
	
	
	@Override
	public Rezervation get(Long id) {
		Rezervation entity = rezervationDao.getById(id);
		return entity;
	}

	@Override
	public void saveOrUpdate(Rezervation rezervation) {
		if (rezervation.getId() == null) {
			LOGGER.debug("Save new: {}",rezervation);
			rezervationDao.insert(rezervation);
		} else {
			LOGGER.debug("Update: {}", rezervation);
			rezervationDao.update(rezervation);
		}
		
	}

	@Override
	public void delete(Rezervation rezervation) {
		LOGGER.debug("Remove: {}", rezervation);
		rezervationDao.delete(rezervation.getId());
		
	}

	@Override
	public void deleteAll() {
		LOGGER.debug("Remove all rezervations");
		rezervationDao.deleteAll();
	}

}
